package com.sz.biz.logistics.core.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.QuotationStart;
import com.sz.biz.logistics.core.service.PriceQuotationStartService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.DateUtils;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Package: com.sz.biz.logistics.core.service.impl <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: penghui.zheng. <br>
 * Date: 2017-03-20 15:00:00.
 */
@Service("PriceQuotationStartService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PriceQuotationStartServiceImpl extends AbstractService implements PriceQuotationStartService {
    @Override
    protected String getMapperNamespace() {
        return "PriceQuotationStartMapper";
    }

    @Override
    public int save(QuotationStart entry) {
        dao.save(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_QUOTATION_START", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_START, UserActions.ADD, "添加报价表运输起点", dataBackup);
        return entry.getId();
    }

    @Override
    public void batchsave(List<QuotationStart> list) {
        dao.batchInsert(getSqlName("insertSelective"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_QUOTATION_START", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_START, UserActions.ADD, "添加报价表运输起点", dataBackup);
    }

    @Override
    public List<QuotationStart> findByQuotationId(int quotationId) {
        ParamData pd = new ParamData();
        pd.put("quotationId", quotationId);
        return dao.findForList(getSqlName("findByQuotationId"), pd, QuotationStart.class);
    }

    /**
     * 根据quotationId查询实体
     *
     * @param quotationIds
     * @return 查询出来的实体对象
     */
    @Override
    public List<QuotationStart> findByQuotationIds(List<Integer> quotationIds) {
        ParamData pd = new ParamData();
        pd.put("quotationIds", quotationIds);
        return dao.findForList(getSqlName("findByQuotationIds"), pd, QuotationStart.class);
    }

    /**
     * 查询所有可用状态的起点
     *
     * @param type type
     * @param time time
     * @return 查询出来的实体对象
     */
    @Override
    public List<QuotationStart> findAllEnableStarts(int type, Date time) {
        ParamData pd = new ParamData();
        if (time != null) {
            pd.put("time", DateUtils.formatDetailDate(time));
        }
        pd.put("type", type);
        return dao.findForList(getSqlName("findAllEnableStarts"), pd, QuotationStart.class);
    }

    @Override
    public QuotationStart findById(Integer id) {
        ParamData pd = new ParamData();
        pd.put("id", id);
        QuotationStart entry = (QuotationStart) dao.findForObject(getSqlName("selectByPrimaryKey"), pd);
        return entry;
    }

    /**
     * 根据quotationId删除起点，用于价格下线编辑价格时直接删除
     *
     * @param quotationId quotationId
     */
    @Override
    public void deletedByQuotationId(Integer quotationId) {
        dao.delete(getSqlName("deletedByQuotationId"), quotationId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_QUOTATION_START", quotationId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_START, UserActions.DELETE, "删除报价表运输起点", dataBackup);
    }
}
